home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d8 / pdriver5.arc / DUMP.C < prev    next >
C/C++ Source or Header  |  1989-12-17  |  5KB  |  234 lines

  1. /* comments are an admission that your code isn't clear enough */
  2.  
  3. /* History:151,1 0 */
  4. /* Wed Aug 09 12:22:30 1989 receive_upcall was dumping two bytes too many */
  5.  
  6. #include <stdio.h>
  7. #include <ctype.h>
  8.  
  9. /* the format of the following struct is dependent upon trace.asm */
  10.  
  11. struct {
  12.     unsigned length;
  13.     unsigned char function;
  14.     unsigned long time;
  15.     unsigned char error;
  16.     union {
  17.         int handle;
  18.         unsigned char bytes[2000];
  19.         struct {
  20.             unsigned version;
  21.             unsigned char class;
  22.             unsigned type;
  23.             unsigned char number;
  24.             unsigned char basic;
  25.         } di;
  26.         struct {
  27.             unsigned char if_class;
  28.             unsigned if_type;
  29.             unsigned char if_number;
  30.             unsigned typelen;
  31.             unsigned handle;
  32.         } at;
  33.         struct {
  34.             unsigned handle;
  35.             unsigned length;
  36.         } ga;
  37.     } data;
  38. } record;
  39.  
  40. char *functions[] = {
  41.     "bad function",
  42.     "driver_info",
  43.     "access_type",
  44.     "release_type",
  45.     "send_pkt",
  46.     "terminate",
  47.     "get_address",
  48.     "reset_interface",
  49.     "set_rcv_mode",
  50.     "get_rcv_mode",
  51.     "set_multicast_list",
  52.     "get_multicast_list",
  53.     "get_statistics",
  54.     "set_address",
  55. };
  56.  
  57. enum {
  58.     bad_function,
  59.     driver_info,
  60.     access_type,
  61.     release_type,
  62.     send_pkt,
  63.     terminate,
  64.     get_address,
  65.     reset_interface,
  66.     set_rcv_mode,
  67.     get_rcv_mode,
  68.     set_multicast_list,
  69.     get_multicast_list,
  70.     get_statistics,
  71.     set_address,
  72.     receive_upcall = 255
  73. };
  74.  
  75. char *errors[] = {
  76.     "no_error",
  77.     "bad_handle",
  78.     "no_class",
  79.     "no_type",
  80.     "no_number",
  81.     "bad_type",
  82.     "no_multicast",
  83.     "cant_terminate",
  84.     "bad_mode",
  85.     "no_space",
  86.     "type_inuse",
  87.     "bad_command",
  88.     "cant_send",
  89.     "cant_set",
  90.     "bad_address",
  91. };
  92.  
  93. main()
  94. {
  95.     FILE *inf;
  96.     static first_time = 1;
  97.     unsigned long time_zero;
  98.     char args[80], results[80];
  99.  
  100.     inf = fopen("trace.out", "rb");
  101.     if (!inf) {
  102.         fprintf(stderr, "dump: cannot open \"trace.out\"\n");
  103.         exit(1);
  104.     }
  105.  
  106.     setvbuf(stdout, NULL, _IOFBF, 4096);
  107.  
  108.     for(;;) {
  109.         if (fread(&record.length, 2, 1, inf) != 1)
  110.             break;
  111.         fread(&record.function, 1, record.length - 2, inf);
  112.         if (first_time) {
  113.             first_time = 0;
  114.             time_zero = record.time;
  115.         }
  116.         args[0] = '\0';
  117.         switch(record.function) {
  118.         case get_statistics:
  119.         case terminate:
  120.         case reset_interface:
  121.         case release_type:
  122.         case get_address:
  123.         case receive_upcall:
  124.             sprintf(args, "%d", record.data.handle);
  125.             break;
  126.         case access_type:
  127.             sprintf(args, "%d %d %d %d", record.data.at.if_class,
  128.                 record.data.at.if_type,
  129.                 record.data.at.if_number,
  130.                 record.data.at.typelen);
  131.             break;
  132.         default:
  133.             strcpy(args, "");
  134.         }
  135.         if (record.error != 0) {
  136.             strcpy(results, errors[record.error]);
  137.         } else switch(record.function) {
  138.         case driver_info:
  139.             sprintf(results, "%d %d %d %d %d",
  140.                 record.data.di.version, record.data.di.class,
  141.                 record.data.di.type, record.data.di.number,
  142.                 record.data.di.basic);
  143.             break;
  144.         case access_type:
  145.             sprintf(results, "%d", record.data.at.handle);
  146.             break;
  147.         case send_pkt:
  148.             sprintf(results, "%d bytes", record.length - 6);
  149.             break;
  150.         case receive_upcall:
  151.             sprintf(results, "%d bytes", record.length - 8);
  152.             break;
  153.         default:
  154.             strcpy(results, errors[0]);
  155.         }
  156.         printf("%7.2f %s(%s) = %s\n",
  157.             (float)(record.time - time_zero) / 18.2,
  158.             record.function == receive_upcall?"receive_upcall":
  159.             functions[record.function], args, results);
  160.         switch(record.function) {
  161.         case get_address:
  162.             dump_bytes(&record.data.bytes[4], record.data.ga.length);
  163.             break;
  164.         case receive_upcall:
  165.             dump_bytes(&record.data.bytes[2], record.length - 8);
  166.             break;
  167.         case send_pkt:
  168.         case set_rcv_mode:
  169.             dump_bytes(&record.data.bytes[0], record.length - 6);
  170.             break;
  171.         }
  172.     }
  173. }
  174.  
  175. dump_bytes(unsigned char *bytes, int count)
  176. {
  177.     int n;
  178.     char buf[16];
  179.     int address;
  180.     void fmtline();
  181.  
  182.     address = 0;
  183.     while(count){
  184.         if (count > 16) n = 16;
  185.         else n = count;
  186.         fmtline(address,bytes,n);
  187.         address += n;
  188.         count -= n;
  189.         bytes += n;
  190.     }
  191. }
  192. /* Print a buffer up to 16 bytes long in formatted hex with ascii
  193.  * translation, e.g.,
  194.  * 0000: 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f  0123456789:;<=>?
  195.  */
  196. void
  197. fmtline(addr,buf,len)
  198. int addr;
  199. char *buf;
  200. int len;
  201. {
  202.     char line[80];
  203.     register char *aptr,*cptr;
  204.     unsigned register char c;
  205.     void ctohex();
  206.  
  207.     memset(line,' ',sizeof(line));
  208.     ctohex(line,addr >> 8);
  209.     ctohex(line+2,addr & 0xff);
  210.     aptr = &line[6];
  211.     cptr = &line[55];
  212.     while(len-- != 0){
  213.         c = *buf++;
  214.         ctohex(aptr,c);
  215.         aptr += 3;
  216.         c &= 0x7f;
  217.         *cptr++ = isprint(c) ? c : '.';
  218.     }
  219.     *cptr++ = '\n';
  220.     fwrite(line,1,(unsigned)(cptr-line),stdout);
  221. }
  222. /* Convert byte to two ascii-hex characters */
  223. static
  224. void
  225. ctohex(buf,c)
  226. register char *buf;
  227. register int c;
  228. {
  229.     static char hex[] = "0123456789abcdef";
  230.  
  231.     *buf++ = hex[c >> 4];
  232.     *buf = hex[c & 0xf];
  233. }
  234.